<!DOCTYPE html>
<html lang="zh-cn">

	<head>
		<meta charset="utf-8">
		<title>code</title>
	</head>

	<body>
		<button style="font-size:32px;color:#ffffff;background-color:#1890ff;" id="hybrid-download-file">下载</button>
		<div id="hybrid-error-msg" style="font-size:48px;color:#27547d;word-break:break-word;"></div>
		<pre><code id="hybrid-code" style="font-size:32px;"></code></pre>
		<!-- uni SDK -->
		<!-- <script type="text/javascript" src="https://js.cdn.aliyun.dcloud.net.cn/dev/uni-app/uni.webview.1.5.2.js"></script> -->
		<!-- highlight.js SDK -->
		<!-- <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/styles/default.min.css"> -->
		<!-- <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/styles/tomorrow-night.min.css"> -->
		<!-- <script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/highlight.min.js"></script> -->
		<link rel="stylesheet" href="css/github.min.css">
		<script src="js/highlight.pack.js"></script>
		<link rel="stylesheet" href="css/highlightjs-line-numbers.css">
		<script src="js/highlightjs-line-numbers.min.js"></script>
		<script>
			hljs.initHighlightingOnLoad();
			hljs.initLineNumbersOnLoad();
		</script>
		<script src="js/axios.min.js"></script>
		<script type="text/javascript">
			// document.addEventListener('UniAppJSBridgeReady', function() {
			// 	当前环境
			// 	uni.getEnv(function(res) {
			// 		console.log('当前环境：' + JSON.stringify(res));
			// 	});
			// });

			/*
			 * 取地址栏的参数 
			 * @param key  
			 * key为传递的参数名称 例如 http://localhost/test.html?p=广东&c=珠海,key就是p和c 
			 * @returns 
			 */
			function getUrlParam(key) {
				// 获取参数    
				var url = window.location.search;
				//window.search 取到的是一串带参数的字符串，如：?p=广东&c=珠海 

				// 正则筛选地址栏    
				var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
				// 匹配目标参数   
				var result = url.substr(1).match(reg);
				//返回参数值    
				return result ? decodeURIComponent(result[2]) : null;
			}

			const owner = getUrlParam('owner')
			const repo = getUrlParam('repo')
			const path = getUrlParam('path')
			const defaultBranch = getUrlParam('defaultBranch')
			axios({
				method: 'get',
				baseURL: 'https://api.github.com',
				url: `/repos/${owner}/${repo}/contents/${path}`,
				responseType: 'text',
				withCredentials: true,
				headers: {
					'Accept': 'application/vnd.github.v3.html+json'
				}
			}).then(res => {
				document.getElementById('hybrid-code').innerHTML = res.data
				setTimeout(function() {
					document.querySelectorAll('pre code').forEach((block) => {
						hljs.highlightBlock(block);
					});
					document.querySelectorAll('code.hljs').forEach((block) => {
						hljs.lineNumbersBlock(block);
					});
				}, 0);
			}).catch(() => {
				document.getElementById('hybrid-error-msg').innerHTML =
					'Sorry about that, but we can’t show files that are this big right now.'
			});
			document.getElementById('hybrid-download-file').addEventListener('click', function(e) {
				window.location.href = `https://raw.githubusercontent.com/${owner}/${repo}/${defaultBranch}/${path}`
			}, false);
		</script>
	</body>
</html>
